package android.mcsmtms.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.mcsmtms.R;
import android.mcsmtms.domain.Place;
import android.mcsmtms.manager.PlacesManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class AboutFragment2 extends Fragment {
	// This is the Adapter being used to display the list's notificationList
	SimpleCursorAdapter mAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_notifications, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		GetPlacesTask getPlacesTask = new GetPlacesTask(this.getActivity(), "police");
		getPlacesTask.execute((Void) null);
	};

	@Override
	public void onPrepareOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onPrepareOptionsMenu(menu);
	}

//	@Override
//	public boolean onOptionsItemSelected(MenuItem item) {
//		switch (item.getItemId()) {
//		case R.id.hospitals:
//			if (item.isChecked())
//				item.setChecked(false);
//			else
//				item.setChecked(true);
//			return true;
//		case R.id.police_stations:
//			if (item.isChecked())
//				item.setChecked(false);
//			else
//				item.setChecked(true);
//			return true;
//		case R.id.evac_centers:
//			if (item.isChecked())
//				item.setChecked(false);
//			else
//				item.setChecked(true);
//		case R.id.user_reports:
//			if (item.isChecked())
//				item.setChecked(false);
//			else
//				item.setChecked(true);
//			return true;
//		default:
//			return super.onOptionsItemSelected(item);
//		}
//	}

	private class GetPlacesTask extends AsyncTask<Void, Void, List<Place>> {

		private ProgressDialog dialog;
		private Context context;
		private String places;

		public GetPlacesTask(Context context, String places) {
			this.context = context;
			this.places = places;
		}

		@Override
		protected List<Place> doInBackground(Void... params) {
			ApplicationInfo ai;
			String mapsApiKey = null;

			try {
				ai = getActivity().getPackageManager().getApplicationInfo(
						getActivity().getPackageName(),
						PackageManager.GET_META_DATA);
				Bundle bundle = ai.metaData;
				mapsApiKey = bundle
						.getString("com.google.android.maps.v2.API_KEY");
			} catch (NameNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.e("Error", "error in getting maps api key");
			}

			PlacesManager placesManager = new PlacesManager(mapsApiKey);
			// 14.57952, 121.04731
			ArrayList<Place> findPlaces = placesManager.findPlaces(14.57952,
					121.04731, places);

			for (int i = 0; i < findPlaces.size(); i++) {
				Place placeDetail = findPlaces.get(i);
				Log.i("Places", "places : " + placeDetail.getName());
			}
			return findPlaces;
		}

		@Override
		protected void onPostExecute(List<Place> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);

			if (dialog.isShowing()) {
				dialog.dismiss();
			}
			
			// For the cursor adapter, specify which columns go into which views
			String[] fromColumns = { "Place Name" };
			int[] toViews = { android.R.id.text1 }; // The TextView in
													// simple_list_item_1

			// Create an empty adapter we will use to display the loaded notificationList.
			// We pass null for the cursor, then update it in onLoadFinished()
			mAdapter = new SimpleCursorAdapter(getActivity(),
					android.R.layout.simple_list_item_1, null, fromColumns,
					toViews, 0);
			// new SimpleCursorAdapter(this,
			// android.R.layout.simple_list_item_1, null,
			// fromColumns, toViews, 0);
			
			//for (int i = 0; i < result.size(); i++) {
				// mMap.addMarker(new MarkerOptions()
				// .title(result.get(i).getName())
				// .position(
				// new LatLng(result.get(i).getLatitude(), result
				// .get(i).getLongitude()))
				// .icon(BitmapDescriptorFactory
				// .fromResource(R.drawable.pin))
				// .snippet(result.get(i).getVicinity()));
				//mAdapter.s
			//}
			
			List placeNameList = new ArrayList<String>();
			for (Place place : result) {
				placeNameList.add(place.getName());
			}
			
			ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, placeNameList);
			 ((ListView) getActivity().findViewById(R.id.listView)).setAdapter(arrayAdapter);
			
			// CameraPosition cameraPosition = new CameraPosition.Builder()
			// .target(new LatLng(result.get(0).getLatitude(), result.get(
			// 0).getLongitude())) // Sets the center of the map to
			// // Mountain View
			// .zoom(14) // Sets the zoom
			// .tilt(30) // Sets the tilt of the camera to 30 degrees
			// .build(); // Creates a CameraPosition from the builder
			// mMap.animateCamera(CameraUpdateFactory
			// .newCameraPosition(cameraPosition));
			
			
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = new ProgressDialog(context);
			dialog.setCancelable(false);
			dialog.setMessage("Loading..");
			dialog.isIndeterminate();
			dialog.show();
		}

	}

}
